package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzcaptcha.contract.IZZSimpleMkvActionService;
import com.bj58.zhuanzhuan.zzcaptcha.entity.CaptchaResult;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.service.IApolloService;
import com.zhuanzhuan.youpin.log.interceptor.LogContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName VerificationCodeHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/11 16:44
 */
@Component
@Slf4j
public class VerificationCodeHelper {

    @Resource
    private IZZSimpleMkvActionService simpleMkvActionService;

    @Resource
    private IApolloService apolloService;

    /**
     * 二次校验验证码
     *
     * @param mobile
     * @param action
     * @param mkvToken
     * @return boolean
     * @author  hanjunjun
     * @date 2024/7/11
     **/
    public boolean verificationCode(String mobile, Integer action, String mkvToken) {
        if (apolloService.queryLogicSwitchConfig().getVerificationCode())  {
            CaptchaResult<Boolean> result =
                simpleMkvActionService.confirmMkvActionToken(LogContext.getLogStr(), mobile, action, mkvToken);
            log.info("confirmMkvActionToken result={}", JsonUtils.toJsonWithoutNull(result));
            if (!result.isSuccess() || Objects.isNull(result.getData())) {
                WxWarnTemplateUtil.warnOutService(Consts.SERVICE_NAME, "scf接口调用失败");
                throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
            }

            return result.getData();
        }

        return true;
    }
}
